#!/bin/bash

#    / **  
#    * 文件名：安装linux软件
#    * 版权： Copyright 谢振斌  All Rights Reserved.  
#    * 修改内容：新增  
#    * 修改人： 谢振斌  
#    * 修改时间：2021-3-23  
#    */  

#    / **  
#    *〈一句话功能简述〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function fun1()
{  
	echo "helld world !"

	# 在shell中：
	# $0: 获取当前脚本的名称
	# $#: 传递给脚本的参数个数
	# $$: shell脚本的进程号
	# $1, $2, $3...：脚本程序的参数
}

#    / **  
#    *〈 判断shell系统类型 〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  String [linux、osx、bsd] 
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function getShellOStype()
{
    SHELL_PLATFORM='unknown';
    ## echo "[Debug] SHELL_PLATFORM=${SHELL_PLATFORM}"

    ## echo $OSTYPE | tr '[A-Z]' '[a-z]'"
    shellostypestring=`echo $OSTYPE`;
    ## echo "[Debug] shellostypestring=${shellostypestring}"

    ## 从字符串中识别shell系统类型
    case "${shellostypestring}" in
        *'linux'*   ) SHELL_PLATFORM='linux'    ;;
        *'darwin'*  ) SHELL_PLATFORM='osx'      ;;
        *'bsd'*     ) SHELL_PLATFORM='bsd'      ;;
    esac

    # if [[ $SHELL_PLATFORM == 'linux' || $SHELL_PLATFORM == 'bsd' ]]; then
    #    echo "[Debug] linux"
    # elif [[ $SHELL_PLATFORM == 'osx' ]]; then
    #    # $SHELL_PLATFORM == 'bsd'
    #    echo "[Debug] osx Shell"
    # elif [[ $SHELL_PLATFORM == 'bsd' || $SHELL_PLATFORM == 'osx'  ]]; then
    #    echo "[Debug] bad shell"
    # else
    #    echo "[Debug] unknown"
    # fi

    # 返回值
    echo $SHELL_PLATFORM;
}


#    / **  
#    *〈获得linux发行版本名称〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function getDistName()
{
	if grep -Eqii "Arch Linux" /etc/issue || grep -Eq "Arch Linux" /etc/*-release; then
        DISTRO='Arch Linux'
        PM='pacman'
    elif grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        PM='apt'
    else
        DISTRO='unknow'
    fi

    # 返回值
    echo $DISTRO;
}



#    / **  
#    *〈 安装软件 从git仓库〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [git仓库地址]  
#    * @param [参数2] [本地存放地址]  
#    * @param [参数3] [克隆后执行的安装命令] 
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function installOrUpdateFormGit()
{
    echo "[INFO] 开始 从git仓库安装软件 "
    pluginsName=$1
    echo "[Debug] pluginsName="${pluginsName}
    pluginsPath=$2
    echo "[Debug] pluginsPath="${pluginsPath}
    echo "[INFO] 安装插件："${pluginsName}
    executableInstallCmdStr=$3
    echo "[Debug] executableInstallCmdStr="${executableInstallCmdStr}

    # 判断是否路径是否已经存在
    if [ ! -d "$pluginsPath" ]; then
      echo "Installing "${pluginsName}
      #URL="https://github.com/zsh-users/"
      URL="https://hub.fastgit.org/"
      if [ -x "$(which git)" ]; then
        # 命令拼接
        #cmdStr="ln -s  "${dir}"/zshrc-oh-my-sh  "${HOME}"/.zshrc"
        cmdStr="git clone "${URL}${pluginsName}" "${pluginsPath}
        echo "执行命令: ${cmdStr}"
        ${cmdStr}
        echo "[INFO] 安装插件"${pluginsName}"安装完成！"
      else
        echo "[ERROR]: 请先安装 git !!"
        exit
      fi
      if [ ! $? -eq 0 ]; then
        echo ""
        echo "[ERROR]: 克隆${pluginsName}仓库中出错!!"
        exit
      else
        # git克隆完成，执行相关安装命令
        echo "git克隆完成，执行命令: ${executableInstallCmdStr}"
        ${executableInstallCmdStr}
      fi
    else
      echo "[INFO] ${pluginsName}已经存在！"
      cmdStr="cd "${pluginsPath}
      echo "执行命令: ${cmdStr}"
      ${cmdStr}

      echo "[INFO] ${pluginsName}已经存在！"
      cmdStr="git pull"
      echo "执行命令: ${cmdStr}"
      ${cmdStr}

      echo "git更新完成，执行命令: ${executableInstallCmdStr}"
      ${executableInstallCmdStr}

    fi
}

#    / **  
#    *〈流程控制〉  
#    *〈该脚本的主函数〉  
#    * @param [无]  
#    * @return [无]  
#    * @exception/throws [无]  
#    * @deprecated  
#    */  
function processControlMain()  
{  
    # 打印当前系统时间  
    echo "Start..."  
    date  

    # 此处是你的代码  
    # ...  
    # fun1
    # 获取当前脚本目录位置
    cd `dirname $0`
    dir=`pwd`
    echo ${dir}

    gitPath="googlehosts/hosts"
    loadPath="${dir}/googlehosts"

    # 获取当前Shell系统类型
    shellos=$(getShellOStype)
    echo "[Debug] shellos = "${shellos}
    echo $shellos;
	if [ "$shellos" == "linux" ]; then
		# 获取当前系统发行版本
		osdist=$(getDistName)
		#echo "[Debug] DISTRO = "
		#echo $DISTRO; 
		echo "[Debug] osdist = "${osdist}
		echo $osdist;
		if [ "$osdist" == "CentOS" ]; then
			echo "[Debug] ======= CentOS"
			# 执行另一个脚本
			# /bin/bash ./zsh1.sh		
		elif [ "$osdist" == "Arch Linux" ] ; then
			echo "[Debug] ======= Arch Linux"
			# 判断是否存在文件夹 没有则克隆，有则进行更新
			#gitPath="googlehosts/hosts"
			#loadPath="${dir}/googlehosts"
			#commandStr="sudo mv /etc/hosts /etc/hosts-bak-$(date +%y%m%d-%H-%M-%S) ; ln -s ${dir}/googlehosts  /etc/hosts"
            commandStr="ls -al"
			installOrUpdateFormGit ${gitPath} ${loadPath} "${commandStr}"
			# 注：参数中带有空格，如果直接写的话，会从空格处自动切分开。解决方法：将这个引用变量用双引号括起来

            # 备份 /etc/hosts 配置文件
            cmdStr="sudo mv /etc/hosts /etc/hosts-bak-$(date +%Y%m%d)"
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
            
            # 建立软链接命令拼接
            cmdStr="sudo ln -s ${loadPath}/hosts-files/hosts /etc/hosts"
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
		else
			echo "[Debug] ======= unknow linux dist";

		fi
    elif [ "$shellos" == "osx" ]; then
        # 
            echo "[Debug] shellos ======= osx ";
            commandStr="ls -al"
            installOrUpdateFormGit ${gitPath} ${loadPath} "${commandStr}"
            # 注：参数中带有空格，如果直接写的话，会从空格处自动切分开。解决方法：将这个引用变量用双引号括起来

            # 备份 /etc/hosts 配置文件
            cmdStr="sudo mv /etc/hosts /etc/hosts-bak-$(date +%Y%m%d)"
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
            
            # 建立软链接命令拼接
            cmdStr="sudo ln  ${loadPath}/hosts-files/hosts /etc/hosts"
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
    else
        echo "[Debug] ======= unknow linux dist";
	fi
	# 执行另一个脚本
	# /bin/bash ./zsh2.sh
    # 打印当前系统时间  
    date  
    echo "The End."  
}  

# 执行入口 
# 进入脚本目录位置
cd `dirname $0`
# 获取当前脚本目录位置
shellfilepath=`pwd`'/'$0
# 打印脚本目录地址
echo "[INFO]: 脚本地址 "${shellfilepath}

if true  
# if false  
then  
    echo "[Debug] true"
    # 开始执行  从标准输入中读取并同时写入到标准输出和指定的文件上 
    ## 拼接日志文件地址
    #logfilepath=`pwd`'/'$0'.log'
    logfilepath=`pwd`'/'$0'-'$(date +%Y%m%d)'.log'
    #logfilepath=`pwd`'/'$0'-'$(date +%y%m%d-%H-%M-%S)'.log'
    ## 打印日志文件地址
    echo "[INFO]: 日志地址" ${logfilepath}
    processControlMain  | tee -a ${logfilepath}
else  
    echo "[Debug] false"  
    ## 函数单元测试  
    #  
fi  

# 脚本结束  
